package cn.lovemmd.model.controller;

import com.alibaba.fastjson2.JSON;
import cn.lovemmd.model.common.Result;
import cn.lovemmd.model.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.lovemmd.model.controller.ModelController.getStringResponseEntity;

/**
 * 统计控制器，提供统计相关接口
 * 
 * @author TLS
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/statistics")
@Api(tags = "统计管理")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class StatisticsController {
    
    private static final Logger log = LoggerFactory.getLogger(StatisticsController.class);
    private final ModelService modelService;

    /**
     * 获取系统总体统计信息
     */
    @GetMapping(value = "/overview", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取系统总体统计信息")
    public ResponseEntity<String> getSystemOverview() {
        try {
            Object statistics = modelService.getModelStats();
            Result<Object> response = Result.success(statistics);
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("获取系统统计信息失败", e);
            Result<String> errorResponse = Result.error("获取系统统计信息失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 获取职业统计
     */
    @GetMapping(value = "/occupation", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取职业统计")
    public ResponseEntity<String> getOccupationStats() {
        try {
            List<Object> stats = modelService.getOccupationStats();
            Result<List<Object>> response = Result.success(stats);
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("获取职业统计失败", e);
            Result<String> errorResponse = Result.error("获取职业统计失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 获取分类统计
     */
    @GetMapping(value = "/category", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取分类统计")
    public ResponseEntity<String> getCategoryStats() {
        try {
            List<Object> stats = modelService.getCategoryStats();
            Result<List<Object>> response = Result.success(stats);
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("获取分类统计失败", e);
            Result<String> errorResponse = Result.error("获取分类统计失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 获取模型总数
     */
    @GetMapping(value = "/total-models", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取模型总数")
    public ResponseEntity<String> getTotalModels() {
        try {
            Long count = modelService.countTotalModels();
            Result<Long> response = Result.success(count);
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("获取模型总数失败", e);
            Result<String> errorResponse = Result.error("获取模型总数失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 获取热门模型（按下载量）
     */
    @GetMapping(value = "/top-models", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取热门模型")
    public ResponseEntity<String> getTopModels(
            @ApiParam("限制数量") @RequestParam(value = "limit", defaultValue = "10") int limit) {
        try {
            var models = modelService.selectTopModels(limit);
            Result<Object> response = Result.success(models);
            return ResponseEntity.ok(JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("获取热门模型失败", e);
            Result<String> errorResponse = Result.error("获取热门模型失败：" + e.getMessage());
            return ResponseEntity.ok(JSON.toJSONString(errorResponse));
        }
    }

    /**
     * 获取职业列表
     */
    @GetMapping(value = "/occupations", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取职业列表")
    public ResponseEntity<String> getOccupationList() {
        return getStringResponseEntity(modelService, log);
    }
}
